package com.xmfh.wemedia.service.impl;

import com.xmfh.wemedia.mapper.DdIndexMapper;
import com.xmfh.wemedia.model.DdArticle;
import com.xmfh.wemedia.service.IDdIndexService;
import com.xmfh.wemedia.util.JsonResult;
import com.xmfh.wemedia.vo.MicroVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 胡桃
 * @Date: 2022/4/9 17:54
 * @Description: com.xmfh.wemedia.service.impl
 * @version: 1.0
 */
@Service
public class DdIndexServiceImpl implements IDdIndexService {
    @Autowired
    private DdIndexMapper indexMapper;

    @Override
    public JsonResult<List<DdArticle>> listAllArticle(String navType) {
        List<DdArticle> articles = new ArrayList<>();
        if ("hot".equals(navType)) {
            articles = indexMapper.listArticleByHotOrderBy();
        }
        if ("recommend".equals(navType)) {
            articles = indexMapper.listArticleByViewDesc();
        }
        return JsonResult.ok(articles);
    }

    @Override
    public JsonResult<List<DdArticle>> listArticleByCondition(String navType, String parameter) {
        List<DdArticle> articles = new ArrayList<>();
        if ("city".equals(navType)) {
            articles = indexMapper.listArticleByPubLocation(parameter);
        }
        if ("category_id".equals(navType)) {
            articles = indexMapper.listArticleByCategoryId(Integer.parseInt(parameter));
        }
        if ("focus_on".equals(navType)) {
            articles = indexMapper.listAllArticleByUid(Integer.parseInt(parameter));
        }
        return JsonResult.ok(articles);
    }

    @Override
    public JsonResult<List<MicroVO>> listMicro(String navType) {
        List<MicroVO> micros = new ArrayList<>();

        if ("recommend".equals(navType)) {
            List<MicroVO> microVOS = indexMapper.listMicroByViewDesc();
            for (MicroVO microVO : microVOS) {
                if (microVO.getContent().length() > 50) {
                    String subContent = microVO.getContent().substring(0, 50);
                    microVO.setContent(subContent);
                }
            }
            micros = microVOS;
        }
        return JsonResult.ok(micros);
    }
}
